<?php

namespace App\Models\Admin;

use App\Models\BaseModel;
use Auth;

class CodeRecord extends BaseModel
{
    protected $fillable = [
        'id', 'store_id', 'user_id', 'phone', 'code',
        'type', 'expire_at', 'created_at', 'updated_at'
    ];
    public function getList($pageSize, $requestStoreId, $phone, $userId)
    {
        $storeId = Auth::guard('admin')->user()->store_id;
        $sql = $this;
        if(!empty($phone)) {
            $sql = $sql->where('phone', 'like', '%'.$phone.'%');
        }
        if(!empty($userId)) {
            $sql = $sql->where('user_id', $userId);
        }
        if ($storeId == 0) {
            // 管理员查看全部店铺验证码 按店铺排序
            if ($requestStoreId == 0) {
                $data = $sql->orderBy('store_id', 'asc')->orderBy('created_at', 'desc')->paginate($pageSize)->toArray();
            } else {
                $data = $sql->where('store_id', $requestStoreId)->orderBy('created_at', 'desc')->paginate($pageSize)->toArray();
            }
        } else {
            // 店家只能查看自己店的验证码
            $data = $sql->where('store_id', $storeId)->orderBy('created_at', 'desc')->paginate($pageSize)->toArray();
        }

        return $this->success($data);
    }

}
